﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

public partial class website_admin_knowlegebase_othersinfoeditor : PageBase
{
    string TitleImage = ConfigurationManager.AppSettings["TitleImage"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin(); 

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        {
            CategoryDataBind();

            if (RequestBase.GetQueryString("articleid") != "")
                Initialize();

        }
    }


    #region 下来菜单数据绑定
    void CategoryDataBind()
    {
        DataTable dt = BLL.CommonBLL.Select("KbCategoryBase", "*", " AND DeletionStateCode = 0 ORDER BY LineNum ASC");
        if (dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'") != null && dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'").Length > 0)
        {
            string categoryid = dt.Select("CategoryNumber='" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryId"].ToString(); 
            DropdownlistDataBind(2, categoryid, dt); 
        }
    }
    /// <summary>
    /// 类别列表邦定
    /// </summary>
    /// <param name="intLayer">级别</param>
    /// <param name="parentId">父目录ID</param>
    /// <param name="dt">目录数据表</param>
    void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
    {
        if (dt == null)
            return;

        DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

        ArrayList al = new ArrayList();
        ListItem li;

        int intCategoryLayerID = 0;
        foreach (DataRow d in dr)
        {
            intCategoryLayerID = int.Parse(d["Layer"].ToString()); 

            li = new ListItem(d["CategoryName"].ToString(), d["CategoryId"].ToString());
            selCategoryList.Items.Add(li);
            DropdownlistDataBind(intCategoryLayerID + 1, d["CategoryId"].ToString().Trim(), dt);
        }
    }
    #endregion

    #region 数据初始化
    void Initialize()
    {
        Model.KbArticleBase _obj = BLL.KbArticleBase.GetObjById(RequestBase.GetQueryString("articleid"));
        ArticleObj = _obj; 
    }

    Model.KbArticleBase ArticleObj
    {
        get
        {
            Model.KbArticleBase _article = new Model.KbArticleBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("articleid")))
                _article.ArticleId = RequestBase.GetQueryString("articleid");

            _article.Title = txtTitle.Value.Trim();
            _article.ArticleNumber = Decimal.Parse(string.IsNullOrEmpty(txtArticleNumber.Value.Trim()) && Utils.IsNumber(txtArticleNumber.Value) ? "0" : txtArticleNumber.Value.Trim());
            _article.CategoryId = selCategoryList.Value;
            _article.Resume = txtResume.Value.Trim();
            _article.Content = txtContent.Text;

            _article.ModifiedOn = DateTime.Now;
            _article.CreatedOn = DateTime.Now;
            _article.ModifiedBy = this.SystemUserId;
            _article.CreatedBy = this.SystemUserId;
            _article.TitlePic = txtUploadControl_TitleImage.Value.Trim().Replace("|", "");

            return _article;
        }
        set
        {
            if (value != null)
            {
                txtTitle.Value = value.Title.Trim();
                txtArticleNumber.Value = value.ArticleNumber.ToString(); 
                txtResume.Value = value.Resume.Trim();
                txtContent.Text = value.Content;
                selCategoryList.Value = value.CategoryId;
                if (value.TitlePic != "")
                {
                    txtUploadControl_TitleImage.Value = "|" + value.TitlePic.Trim();
                    divProductImagesList_TitleImage.InnerHtml = "<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(value.TitlePic.Trim().Split('*')[2]) + "&nbsp;&nbsp;<a href='../../" + TitleImage + value.TitlePic.Trim().Split('*')[1] + "' title='Click to open the file' target='_blank'>" + value.TitlePic.Trim().Split('*')[0] + "</a>&nbsp;&nbsp; <a href='javascript:void(0);' filedata='" + value.TitlePic.Trim() + "' itemid='TitleImage' onclick='uploadinit($(this));'>[Delete]</a></div>";
                }
            }
        }
    } 
    #endregion

    #region 保存数据
    void Save()
    {
        string res = "";
 
        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            object _obj = BLL.Search.CommonSearch.ExecScalar("KbArticleBase",
                    "TOP 1 ArticleId",
                    new string[] { "CategoryId", "DeletionStateCode" },
                    new string[] { "Equal", "Equal" },
                    new string[] { "'" + selCategoryList.Value.Trim() + "'", "0" },
                    null, null
                );

            if (_obj != null && Utils.IsGuid(_obj.ToString()))
            {
                Model.KbArticleBase _article = ArticleObj;
                _article.ArticleId = _obj.ToString();
                res = BLL.KbArticleBase.Update(_article);
            }
            else
                res = BLL.KbArticleBase.Add(ArticleObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.KbArticleBase.Update(ArticleObj);
        }
 
        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("Information saved successfully!", "window.opener.LoadingData(); window.close();");
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
